1. Function Implementations

1.1 function 1: ReadFile and attach packages

1.2. function 2: Wave

1.3. function 3: fourier_smooth

1.4. function 4: fPCA.nodes

1.5. function 5: node.scaler

1.6. function 6: row.check

main script

2. Examples of using each function

2.1 Read data

file_loc1 = '/Users/hanwang/desktop/Git_desktop/Functional_Data_Analysis/data_15may2020.csv'
file_loc5 = '/Users/hanwang/desktop/Git_desktop/Functional_Data_Analysis/Data/Patient5.csv'
file_loc9 = '/Users/hanwang/desktop/Git_desktop/Functional_Data_Analysis/Data/Patient9.csv'
file_loc28 = '/Users/hanwang/desktop/Git_desktop/Functional_Data_Analysis/Data/Patient28.csv'
file_loc80 = '/Users/hanwang/desktop/Git_desktop/Functional_Data_Analysis/Data/Patient80.csv'

patient1 <- ReadFile(file_loc1, time_subset=c(1:600), node_subset=c(1:32))
patient5 <- ReadFile(file_loc5, time_subset=c(1:600), node_subset=c(1:32))
patient9 <- ReadFile(file_loc9, time_subset=c(1:600), node_subset=c(1:32))
patient28 <- ReadFile(file_loc28, time_subset=c(1:600), node_subset=c(1:32))
patient80 <- ReadFile(file_loc80, time_subset=c(1:600), node_subset=c(1:32))

read in data from the specified file location

2.2 Wave function -> single node

# extract periodic curves from a single node (before smoothing)
node1 = Wave(patient5[,1], register=1)
ggplot(node1, aes(time, y_value,group=cycle, colour=cycle)) + geom_line() + theme(legend.position="right")+ ggtitle("Original")

### 2.3(a) node 1 of Patients 1

# apply node.scaler to *single node* and apply row.check match row numbers
a = node.scaler(patient1, node=c(1), k=32)
b = row.check(node_data=a)
b = read.zoo(b, index='index')
autoplot(b, facet = NULL, main='After scaling')

# fPCA
rotpcalist = fPCA.nodes(as.matrix(b), k=11, nharm=2, plt=1)

2.3(b) node 1 of Patients 9

# apply node.scaler to *single node* and apply row.check match row numbers
a = node.scaler(patient9, node=c(1), k=32)
b = row.check(node_data=a)
b = read.zoo(b, index='index')
autoplot(b, facet = NULL, main='After scaling')

# fPCA
rotpcalist = fPCA.nodes(as.matrix(b), k=11, nharm=2, plt=1)

3. Patient 1, Variance among all nodes on the two principle components

# fPCA on all nodes
PC1_df = data.frame(matrix(nrow=80))
PC2_df = data.frame(matrix(nrow=80))
mean_df = data.frame(matrix(nrow=80))
for(node in 1:32){
  node.df = node.scaler(patient1, node, k=32)
  node.df = as.matrix(row.check(node.df))
  rotpcalist = fPCA.nodes(data_mat=node.df, k=11, nharm=2, plt=0)
  # PC1 & PC2
  harmfd <- rotpcalist[[1]]
  basisfd <- harmfd$basis
  rangex <- basisfd$rangeval
  x <- seq(rangex[1], rangex[2], length = harmfd$basis$rangeval[2])
  fdmat <- eval.fd(x, harmfd)
  meanmat <- eval.fd(x, rotpcalist$meanfd)
  PC1_df[,ncol(PC1_df)+1]=fdmat[,1]    
  PC2_df[,ncol(PC2_df)+1]=fdmat[,2]
  mean_df[,ncol(mean_df)+1]=meanmat
}
PC1_df = data.frame(PC1_df[,2:(ncol(PC1_df))])
names(PC1_df)=colnames(patient1)
PC2_df = data.frame(PC2_df[,2:(ncol(PC2_df))])
names(PC2_df)=colnames(patient1)
mean_df = data.frame(mean_df[,2:(ncol(mean_df))])
names(mean_df)=colnames(patient1)
## plot
### PC1
z_1 = read.zoo(PC1_df, index='index')
### PC2
z_2 = read.zoo(PC2_df, index='index')
### Mean Functional Curves
z_3 = read.zoo(mean_df, index='index')
autoplot(z_1, facet = NULL, main='PC1')

autoplot(z_2, facet = NULL, main='PC2')

autoplot(z_3, facet = NULL, main='mean curves')

3(b). Patient 28, Variance among all nodes on the two principle components

# fPCA on all nodes
PC1_df = data.frame(matrix(nrow=80))
PC2_df = data.frame(matrix(nrow=80))
mean_df = data.frame(matrix(nrow=80))
for(node in 1:32){
  node.df = node.scaler(patient28, node, k=32)
  node.df = as.matrix(row.check(node.df))
  rotpcalist = fPCA.nodes(data_mat=node.df, k=11, nharm=2, plt=0)
  # PC1 & PC2
  harmfd <- rotpcalist[[1]]
  basisfd <- harmfd$basis
  rangex <- basisfd$rangeval
  x <- seq(rangex[1], rangex[2], length = harmfd$basis$rangeval[2])
  fdmat <- eval.fd(x, harmfd)
  meanmat <- eval.fd(x, rotpcalist$meanfd)
  fac1 <- sqrt(rotpcalist$values[1]) 
  fac2 <- sqrt(rotpcalist$values[2]) 
  PC1_df[,ncol(PC1_df)+1]=2*fac1*fdmat[,1]    
  PC2_df[,ncol(PC2_df)+1]=2*fac2*fdmat[,2]
  mean_df[,ncol(mean_df)+1]=meanmat
}
PC1_df = data.frame(PC1_df[,2:(ncol(PC1_df))])
names(PC1_df)=colnames(patient1)
PC2_df = data.frame(PC2_df[,2:(ncol(PC2_df))])
names(PC2_df)=colnames(patient1)
mean_df = data.frame(mean_df[,2:(ncol(mean_df))])
names(mean_df)=colnames(patient1)
## plot
### PC1
z_1 = read.zoo(PC1_df, index='index')
### PC2
z_2 = read.zoo(PC2_df, index='index')
### Mean Functional Curves
z_3 = read.zoo(mean_df, index='index')
autoplot(z_1, facet = NULL, main='PC1')

autoplot(z_2, facet = NULL, main='PC2')

autoplot(z_3, facet = NULL, main='mean curves')